//
//  MSSearchPriceVC.m
//  MS
//
//  Created by Yuan on 13-3-11.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSSearchPriceVC.h"
#import "CustomSegmentedControl.h"
#import "UIColor+ColorUtils.h"
#import "PriceView.h"
#import "CustomUIView.h"
#import "MSSearchPriceResult.h"


@interface MSSearchPriceVC ()<CustomSegmentedControlDelegate>
{
    NSInteger selectType;
    NSInteger selectPrice;
        
    CustomUIView *couponsView;
}
@end

@implementation MSSearchPriceVC

- (id)init
{
    self = [super init];
    if (self)
    {
        selectType = 1;
        selectPrice = 0;
    }
    
    return self;
}


-(void)loadView
{
    [super loadView];
    
    UIImage *bgImage = [UIImage imageNamed:@"search_price_bg"];
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - bgImage.size.width/4, 30,bgImage.size.width/2, bgImage.size.height/2)];
    [bgImageView setImage:bgImage];
    bgImageView.alpha = 1.0;
    bgImageView.userInteractionEnabled = YES;
    [self.view addSubview:bgImageView];
    
    UILabel *typeLbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 18,60, 30)];
    typeLbl.text = HYLocalizedString(@"我想要");
    typeLbl.font = FONT_NORMAL;
    typeLbl.backgroundColor = [UIColor clearColor];
    [bgImageView addSubview:typeLbl];
    
    NSArray *arr = [NSArray arrayWithObjects:HYLocalizedString(@"洗剪吹"),HYLocalizedString(@"烫发"),HYLocalizedString(@"染发"),HYLocalizedString(@"护理"), nil];
    UIView *typeView = [self typeView:arr frame:CGRectMake(typeLbl.frame.origin.x+typeLbl.frame.size.width-8, typeLbl.frame.origin.y+5, bgImageView.frame.size.width, 50) selected:selectType-1];
    [bgImageView addSubview:typeView];
    
    UILabel *moneyLbl = [[UILabel alloc]initWithFrame:CGRectMake(typeLbl.frame.origin.x,typeLbl.frame.size.height + typeLbl.frame.origin.y + 24,60, 30)];
    moneyLbl.text = HYLocalizedString(@"预算(元)");
    moneyLbl.font = FONT_NORMAL;
    moneyLbl.backgroundColor = [UIColor clearColor];
    [bgImageView addSubview:moneyLbl];
    
    
    NSArray *moneys = [NSArray arrayWithObjects:HYLocalizedString(@"小于100"),HYLocalizedString(@"100-200"),HYLocalizedString(@"200-300"),HYLocalizedString(@"大于300"), nil];
    PriceView *priceView = [[PriceView alloc]initWithFrame:CGRectMake(typeView.frame.origin.x - 20, moneyLbl.frame.origin.y-22, bgImageView.frame.size.width-40, 100) withArray:moneys selected:selectPrice];
    [priceView setPriceViewBlock:^(int tag,NSString *info)
    {
        NSLog(@"%@----%d",info,tag-1);
        selectPrice = tag-1;
    }];
    [bgImageView addSubview:priceView];
    
    
    UIImage *image = [UIImage imageNamed:@"button_bg"];    
    CGFloat btnWidth = 130.0;
    UIButton *searchBtn = [MSTool createButton:CGRectMake(95,bgImageView.frame.size.height + bgImageView.frame.origin.y + 32,btnWidth, image.size.height+10) action:@selector(searchPrice:) delegate:self];
    [searchBtn setBackgroundImage:[image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2] forState:UIControlStateNormal];
    [searchBtn setTitle:HYLocalizedString(@"确定") forState:UIControlStateNormal];
    [self.view addSubview:searchBtn];
    
    couponsView = [[CustomUIView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:couponsView];
    [couponsView setHidden:YES];
    
}

-(UIView *)typeView:(NSArray *)arr frame:(CGRect)frame selected:(int)selected
{
    UIView *typeview = [[UIView alloc]initWithFrame:frame];
    CGFloat offset_x = 0.0;
    for (int i = 0; i < [arr count]; i++)
    {
        UIImage *noramImage = [UIImage imageNamed:@"search_price_normal"];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(offset_x,0, 0,0)];
        btn.backgroundColor = [UIColor clearColor];
        [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        [btn.titleLabel setFont:FONT_NORMAL];
        [btn setTitleColor:[UIColor colorWithString:@"#f376a0"] forState:UIControlStateNormal];
        [btn setImage:noramImage forState:UIControlStateNormal];
        [btn setImage:noramImage forState:UIControlStateHighlighted];
        [btn setImage:[UIImage imageNamed:@"search_price_selected"] forState:UIControlStateSelected];
        btn.tag = i;
        [btn addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
        [typeview addSubview:btn];
        [btn sizeToFit];
        offset_x += btn.frame.size.width+2;
        if (selected==i) {
            btn.selected = YES;
        }
    }
    return typeview;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad
{
    [self setTitle:@"查价位" showBackButton:YES];
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}


-(void)configureViews:(BOOL)animation
{
    [super configureViews:animation];
    
    //custom segment
    NSDictionary *item1 = [NSDictionary dictionaryWithObjectsAndKeys:HYLocalizedString(@"查价位"),@"title",@"s1",@"img", nil];
    //    NSDictionary *item2 = [NSDictionary dictionaryWithObjectsAndKeys:HYLocalizedString(@"通知"),@"title",@"s2",@"img", nil];
    NSDictionary *item3 = [NSDictionary dictionaryWithObjectsAndKeys:HYLocalizedString(@"找优惠"),@"title",@"s3",@"img", nil];
    NSArray *items = [NSArray arrayWithObjects:item1,item3, nil];
    CustomSegmentedControl *segment = [[CustomSegmentedControl alloc]initWithFrame:CGRectZero items:items defaultSelectIndex:0];
    segment.delegate = self;
    self.navigationItem.titleView=segment;
    
}


#pragma CustomSegmentedControlDelegate methord
-(void)didSelectIndex:(NSUInteger)selectedIndex withSegmentControl:(CustomSegmentedControl*)segmentControl
{
    couponsView.hidden = !selectedIndex;
}


-(void)selectType:(UIButton *)btn
{
    [btn.superview.subviews makeObjectsPerformSelector:@selector(setSelected:) withObject:(id)kCFBooleanFalse];
    btn.selected = !btn.selected;
    selectType = btn.tag + 1;
}

-(void)searchPrice:(UIButton *)btn
{
    NSDictionary *p =  [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt:selectPrice],@"keyword",
                        [NSNumber numberWithInt:selectType],@"classification",
                        nil];
    MSSearchPriceResult *vc = [[MSSearchPriceResult alloc]initWithInfo:p];
    [self.navigationController pushViewController:vc animated:YES];    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
